package io.renren.modules.sys.service.impl;

import io.renren.common.utils.JsonUtils;
import io.renren.common.constant.Constants;
import io.renren.common.exception.RRException;
import io.renren.common.utils.FileUploadUtils;
import io.renren.modules.sys.request.SysAttachmentDelRequest;
import io.renren.modules.sys.request.SysAttachmentPlayRequest;
import io.renren.modules.sys.request.SysAttachmentUploadImageRequest;
import io.renren.modules.sys.service.SysAttachmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.io.IOException;
import java.util.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.modules.sys.dao.SysAttachmentDao;
import io.renren.modules.sys.entity.SysAttachmentEntity;
import io.renren.modules.sys.request.SysAttachmentPageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
public class SysAttachmentServiceImpl extends ServiceImpl<SysAttachmentDao, SysAttachmentEntity> implements SysAttachmentService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Map queryPage(SysAttachmentPageRequest request) {
        Page<SysAttachmentEntity> page = new Page<SysAttachmentEntity>(request.getPage() > 0 ? request.getPage() : 0, request.getLimit() > 1 ? request.getLimit() : 10);

        QueryWrapper<SysAttachmentEntity> queryWrapper = new QueryWrapper<SysAttachmentEntity>();
        queryWrapper.eq("SYS_USER_ID", request.getUserId());
        queryWrapper.orderByDesc("SORT");
        IPage<SysAttachmentEntity> pageInfo = baseMapper.selectPage(page, queryWrapper);
        Map map = new HashMap();
        map.put("total", pageInfo.getTotal());
        map.put("items", pageInfo.getRecords());

        return map;
    }

    /***
     * 图片上传
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public SysAttachmentEntity uploadImage(SysAttachmentEntity entity) throws RRException {
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void remove(SysAttachmentDelRequest request) throws RRException {
        String filePath = stringRedisTemplate.opsForValue().get(Constants.SYS_CONFIG + Constants.FILE_PATH);
        if (StringUtils.isBlank(filePath)) {
            throw new RRException("文件上传路径");
        }
        Map mapFilePath = (Map) JsonUtils.valueAsObject(filePath, Map.class);
        if (StringUtils.isBlank(mapFilePath.get("value") + "")) {
            throw new RRException("文件上传路径");
        }

        SysAttachmentEntity entity = baseMapper.selectById(request.getId());
        baseMapper.deleteById(request.getId());
        FileUploadUtils.delImgUrl(mapFilePath.get("value") + entity.getUrlPath());

    }

    /***
     * 视频播放
     * @param request
     * @return
     * @throws RRException
     */
    @Override
    public Map play(SysAttachmentPlayRequest request) throws RRException {
        Map map = new HashMap();
        SysAttachmentEntity entity = baseMapper.selectById(request.getId());
        map.put("SysAttachmentEntity", entity);
        return map;
    }

}
